﻿using System;
using System.Collections.Generic;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Text;
using ReporterControlLibrary.Models;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.ComponentModel;
using System.Xml;
using System.Xaml;
using System.Windows.Input;
using Microsoft.Expression.Interactivity.Core;
using ReporterSystem;

namespace Reporter.Data
{
    public class AppData
    {
        private static int projectsCount = 1;
        private static int reportInfosCount = 1;

        public static ObservableCollection<string> DateFormats = new ObservableCollection<string> { "yyyy-mm-dd", "yy-mm-dd", "dd-mm-yyyy", "dd-mm-yy" };

        private static ObservableCollection<IProject> _projects = new ObservableCollection<IProject>();

        public static ObservableCollection<IProject> Projects
        {
            get { return _projects; }
        }

        private static ObservableCollection<InstanceNode> _instanceNodes = new ObservableCollection<InstanceNode>();

        public static ObservableCollection<InstanceNode> InstanceNodes
        {
            get { return _instanceNodes; }
        }

        static AppData()
        {
            // Create object for Project Home Tab
            ProjectHome ph = new ProjectHome();
            Projects.Add(ph);

            
        }

        public static void ReportTemplate(Guid templateId)
        {
            
        }

        public static ReportInfo GetReportInfoByID(Guid id)
        {
            foreach(Project project in Projects.Where(x=>x is Project))
            {
                
                var repinfo = project.ReportInfos.Where(x => x.ReportTemplate.ReportTemplateID == id).FirstOrDefault();

                if(repinfo != null)
                {
                    return repinfo;
                }
            }
            
            return null;
        }


        public static void LoadProject(string projectInfoFilePath)
        {
            //TODO: implementuj obsluhu nacitania projektu zo suboru
        }

        public static bool ImportProvisionNotebook(Project project, bool importImages)
        {
            //TODO: implementuj obsluhu importovania provision notebooku
            return ReportApp.ImportProvision(project.ReportProject, importImages);
        }

        public static void RemoveProject(Project project)
        {
            if (project != null && Projects.Contains(project))
            {
                Projects.Remove(project);
            }
        }

        public static void RemoveAllProjects()
        {
            foreach (var item in Projects.ToList().Where(x => x is Project))
            {
                Projects.Remove(item);
            }
        }

        public static void AddProject(ReportProject reportProject)
        {
            var proj = MapProject(reportProject);
            Projects.Add(proj);
        }

        //public static Project AddNewProject()
        //{
        //    var p = GenerateSampleProject();
        //    Projects.Add(p);
        //    projectsCount++;
        //    return p;
        //}

        public static void CreateNewReportInfo(Project project)
        {
            if (project != null)
            {
                var rinfo = new ReportInfo {Id = Guid.NewGuid(), ReportLogs = new EntityCollection<ReportLog>()};

                ReportTemplate rTemplate = new ReportTemplate();
                rinfo.ReportTemplate = rTemplate;

                ReportApp.CreateTemplate(rTemplate,project.ReportProject);

                project.ReportInfos.Add(rinfo);
            }
        }

        public static void LoadData()
        {
            var myProjects = ReportApp.LoadMyProjects();

            foreach(var myProj in myProjects)
            {
                AddProject(myProj);
            }




        
        }

        public static Project MapProject(ReportProject reportProject)
        {
            var result = new Project
                             {
                                 ProjectInfo = new ProjectInfo(),
                                 ReportInfos = new EntityCollection<ReportInfo>(),
                                 ProjectPreferences = new ProjectPreferences(),
                                 ReportProject = reportProject,
                                 Name = reportProject.Name
                             };

            result.ProjectInfo.NotebookName = reportProject.Notebook.NotebookName;
            result.ProjectInfo.ImportedDate = reportProject.Notebook.LastImportDate == DateTime.MinValue ? null : (DateTime?)reportProject.Notebook.LastImportDate;
            result.ProjectInfo.ProvisionFileVersion = reportProject.Notebook.ProvisionVersion;

            if(result.MetaModel == null)
                result.MetaModel = new MetaModel();

            result.MetaModel.EvalExpression = reportProject.EvalPath;

            foreach(var tmp in reportProject.Templates)
            {
                var newTmp = new ReportInfo();

                newTmp.AutoOpenResult = tmp.AutoOpenResult;

                newTmp.TemplateName = tmp.TemplateFileName;
                newTmp.TemplateFile = tmp.TemplateFilePath;

                newTmp.ResultName = tmp.ResultFileName;
                newTmp.ResultFile = tmp.ResultFilePath;

                //map real data to design object
                newTmp.ReportTemplate = tmp;
                
                result.ReportInfos.Add(newTmp);

                newTmp.LastReportDate = tmp.ReportedDate;

                foreach (var parameter in tmp.Parameters)
                {
                    newTmp.ReportParameters.Add(new ReportParameter { Name = parameter .ParameterName, Value = parameter.Value});
                }

            }

            //TODO: 


            return result;
        }

     

       
    }
}
